#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QFile>
#include <QDateTime>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
   QString path = QFileDialog::getExistingDirectory(this,
                                      "get Dir",
                                      QDir::currentPath());
   if(path.isEmpty()) return;

    QDir dir(path);//构造目录类对象
    QFileInfoList info_list = dir.entryInfoList(QDir::Files);//获取目录下的文件列表,文件类型过滤器，排序顺序；
for(int i = 0;i <info_list.size();i++){
   QFileInfo fileInfo = info_list.at(i);//获取下标为i的文件
   //文件名 大小，最后一次修改时间；
//   QDateTime lastModified = fileInfo.lastModified();

//   // 使用 QString::fromLatin1() 或者 QStringLiteral 定义格式字符串
//   QString formattedLastModified = lastModified.toString("yyyy-MM-dd hh:mm:ss");

   QString formattedLastModified = fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss");
    ui->textEdit->append(fileInfo.fileName() + " " + QString::number(fileInfo.size())+"time:"+formattedLastModified);

}

}

void Widget::on_pushButton_2_clicked()
{
    QString path = QFileDialog::getOpenFileName(this,"open",QDir::currentPath(),"文本文件(*.txt *.word *.cpp *.c)");
    if(path.isEmpty()) return;
    QFile file(path);
    if(!file.open(QIODevice::ReadOnly)) return;
    ui->textEdit_2->setText(file.readAll());
    file.close();
}

void Widget::on_pushButton_3_clicked()
{
    QString path = QFileDialog::getSaveFileName(this,"save",QDir::currentPath(),"文本文件(*.txt *.word *.cpp *.c)");
     if(path.isEmpty()) return;
      QFile file(path);
     if(!file.open(QIODevice::WriteOnly)) return;
    file.write(ui->textEdit_2->toPlainText().toLatin1());
}
